import {
	get,
	post
} from '@/utils/http'

//采集地址
const baseurl = 'https://api.okzy.tv/api.php/provide/vod/at/json/'
//最新
const newestData = async (pg, h) => {
	let {
		list,
		pagecount
	} = await get(baseurl, {
		ac: 'detail',
		pg,
		h
	})
	return {
		type_id: null,
		type_name: '最新',
		list,
		pagecount
	}
}
//电影
const movieDataList = async () => {
	let movieDataList = [{
		type_id: 6,
		type_name: "动作片",
		list: []
	}, {
		type_id: 7,
		type_name: "喜剧片",
		list: []
	}, {
		type_id: 8,
		type_name: "爱情片",
		list: []
	}, {
		type_id: 9,
		type_name: "科幻片",
		list: []
	}, {
		type_id: 10,
		type_name: "恐怖片",
		list: []
	}, {
		type_id: 11,
		type_name: "剧情片",
		list: []
	}, {
		type_id: 12,
		type_name: "战争片",
		list: []
	}, {
		type_id: 20,
		type_name: "纪录片",
		list: []
	}, {
		type_id: 37,
		type_name: "伦理片",
		list: []
	}, {
		type_id: 21,
		type_name: "微电影",
		list: []
	}, {
		type_id: 36,
		type_name: "电影解说",
		list: []
	}]

	for (let i = 0; i < movieDataList.length; i++) {
		let item = movieDataList[i]
		let {
			list
		} = await get(baseurl, {
			ac: 'detail',
			t: item.type_id
		})
		list = list.slice(0, 9)
		item.list = list
	}
	return movieDataList
}
//连续剧
const sitcomDataList = async () => {
	let sitcomDataList = [{
		type_id: 13,
		type_name: "国产剧",
		list: []
	}, {
		type_id: 14,
		type_name: "香港剧",
		list: []
	}, {
		type_id: 15,
		type_name: "韩国剧",
		list: []
	}, {
		type_id: 16,
		type_name: "欧美剧",
		list: []
	}, {
		type_id: 22,
		type_name: "台湾剧",
		list: []
	}, {
		type_id: 23,
		type_name: "日本剧",
		list: []
	}, {
		type_id: 24,
		type_name: "海外剧",
		list: []
	}]

	for (let i = 0; i < sitcomDataList.length; i++) {
		let item = sitcomDataList[i]
		let {
			list
		} = await get(baseurl, {
			ac: 'detail',
			t: item.type_id
		})
		list = list.slice(0, 9)
		item.list = list
	}
	return sitcomDataList
}
//动漫
const comicDataList = async () => {
	let comicDataList = [{
		type_id: 29,
		type_name: "国产动漫",
		list: []
	}, {
		type_id: 30,
		type_name: "日韩动漫",
		list: []
	}, {
		type_id: 31,
		type_name: "欧美动漫",
		list: []
	}, {
		type_id: 32,
		type_name: "港台动漫",
		list: []
	}, {
		type_id: 33,
		type_name: "海外动漫",
		list: []
	}]

	for (let i = 0; i < comicDataList.length; i++) {
		let item = comicDataList[i]
		let {
			list
		} = await get(baseurl, {
			ac: 'detail',
			t: item.type_id
		})
		list = list.slice(0, 9)
		item.list = list
	}
	return comicDataList
}
//综艺
const varietyDataList = async () => {
	let varietyDataList = [{
		type_id: 25,
		type_name: "内地综艺",
		list: []
	}, {
		type_id: 26,
		type_name: "港台综艺",
		list: []
	}, {
		type_id: 27,
		type_name: "日韩综艺",
		list: []
	}, {
		type_id: 28,
		type_name: "欧美综艺",
		list: []
	}]

	for (let i = 0; i < varietyDataList.length; i++) {
		let item = varietyDataList[i]
		let {
			list
		} = await get(baseurl, {
			ac: 'detail',
			t: item.type_id
		})
		list = list.slice(0, 9)
		item.list = list
	}
	return varietyDataList
}

//全部分类
const classDataList = async () => {
	let response = await get(baseurl, {
		ac: 'list'
	})
	return response['class']
}

//列表
const listData = async (pg, t) => {
	let classList = await classDataList()
	let {
		type_name
	} = classList.find(item => item.type_id == t)
	let {
		list,
		pagecount
	} = await get(baseurl, {
		ac: 'detail',
		pg,
		t
	})
	return {
		type_id: null,
		type_name,
		list,
		pagecount
	}
}

//详情
const detailData = async (id) => {
	let {
		list
	} = await get(baseurl, {
		ac: 'detail',
		ids: id
	})
	list[0].vod_down_url = list[0].vod_down_url.split('#').map(item => {
		let name = item.split('$')[0]
		let url = item.split('$')[1]
		return {
			name,
			url
		}
	})
	list[0].vod_play_from = list[0].vod_play_from.split('$$$')[1]
	list[0].vod_play_note = list[0].vod_play_note.split('$$$')[1]
	list[0].vod_play_server = list[0].vod_play_server.split('$$$')[1]
	list[0].vod_play_url = list[0].vod_play_url.split('$$$')[1].split('#').map(item => {
		let name = item.split('$')[0]
		let url = item.split('$')[1]
		return {
			name,
			url
		}
	})
	return list[0]

}
//通知
const notifyData = async () => {
	return '本站所有视频和图片均来自互联网收集而来，版权归原创者所有，本网站只提供web页面服务，并不提供资源存储，也不参与录制、上传。'
}

//获取播放历史记录列表
const playHistoryListData = async ()=>{
	let playHistoryList = uni.getStorageSync('play_history_list')
	if (playHistoryList && playHistoryList.length > 0) {
		return playHistoryList
	}
	return []
}

//播放历史记录
const playHistoryData = async (id) => {
	let playHistoryList = uni.getStorageSync('play_history_list')
	if (playHistoryList && playHistoryList.length > 0) {
		return playHistoryList.find(item => item.id == id)
	}
	return null
}

//保存播放历史记录
const savePlayHistoryData = async ({
	id,
	detailData,
	currentDramaData,
	currentTime
}) => {
	let playHistoryList = uni.getStorageSync('play_history_list')
	if (playHistoryList && playHistoryList.length > 0) {
		playHistoryList = playHistoryList.filter(item => item.id != id)
		playHistoryList.push({
			id,
			detailData,
			currentDramaData,
			startTime: currentTime
		})
		uni.setStorageSync('play_history_list', playHistoryList)
	} else {
		uni.setStorageSync('play_history_list', [{
			id,
			detailData,
			currentDramaData,
			startTime: currentTime
		}])
	}

}
//删除历史记录
const delPlayHistoryData = async (id) => {
	let playHistoryList = uni.getStorageSync('play_history_list')
	playHistoryList = playHistoryList.filter(item => item.id != id)
	uni.setStorageSync('play_history_list', playHistoryList)
}

//搜索结果
const searchData = async (pg, search) => {
	let {
		list,
		pagecount
	} = await get(baseurl, {
		ac: 'detail',
		wd: search,
		pg
	})
	return {
		type_id: null,
		type_name: '搜索结果',
		list,
		pagecount
	}

}
export default {
	notifyData,
	newestData,
	movieDataList,
	sitcomDataList,
	comicDataList,
	varietyDataList,
	listData,
	detailData,
	playHistoryData,
	savePlayHistoryData,
	searchData,
	playHistoryListData,
	delPlayHistoryData
}
